<?php
// +----------------------------------------------------------------------
// | 通用PHP框架 [ ThinkPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 郑州智众 [Paulxun@qq.com]
// +----------------------------------------------------------------------
// | 官方网站: https://www.zzmind.com
// +----------------------------------------------------------------------

namespace app\user\model;

use app\user\model\Area as AreaModel;
use think\Model;
use think\facade\Db;

/**
 * Vip会员用户模型
 * @package app\user\model
 */
class Vip extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'user_vip';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
	
	// 错误信息
	protected $error;
	
	// 用户信息
	protected $user = [];
    
	/**
	 * 获取Vip列表
	 * @return array|mixed
	 */
	public static function getList($map = [], $order = [], $paginate = 0)
	{
		if ($paginate == 0) $paginate = config('paginate');
	    $data_list = self::view('user_vip', true)
	        ->view("user_member", ['openid', 'nickname', 'avatar_img'], 'user_member.id=user_vip.member_id', 'left')
	        ->where($map)
	        ->order($order)
	        ->paginate($paginate);
			
	    return $data_list;
	}
	/**
	 * 获取用户地址
	 * @param int $uid 会员id
	 * @author paulxun
	 * @return array|static
	 */
	public static function getAddress($uid = 99)
	{
		$data = cache('vip_address_'.$uid);
		if (!$data) {
			$vip = self::where('member_id', '=', $uid)->find();
			if (!$vip) return [];
			$data = [
				'name'	=> $vip['realname'],
				'mobile'	=> $vip['mobile'],
				'province' => AreaModel::name($vip['province']),
				'city' => AreaModel::name($vip['city']),
				'country' => AreaModel::name($vip['country']),
				'postalcode' => $vip['postalcode'],
				'address'	=> $vip['address'],
				'complete'	=> true,
			];
			if ($data['name'] == '' || $vip['mobile'] == '' || $vip['province'] == 0) $data['complete'] = false;
			cache('vip_address_'.$uid, $data);
		}
		return $data;
	}
	
	/**
	 * 缓存获取所有后代id
	 * @param int $pid 父级id
	 * @author PaulXun <paulxun@qq.com>
	 * @return array
	 */
	public static function getChilds($pid = 0)
	{
		$ids = cache('vip_childs_'.$pid);
		if (!$ids) {
			$ids = self::getChildsId($pid);
			// 非开发模式，缓存数据
			if (config('app.develop_mode') == 0) {
			    cache('vip_childs_'.$pid, $ids, 3600);
			}
		}
	    return $ids;
	}
	/**
	 * 获取所有后代id
	 * @param int $pid 父级id
	 * @author PaulXun <paulxun@qq.com>
	 * @return array
	 */
	public static function getChildsId($pid = 0)
	{
	    $ids = self::where('pid', $pid)->column('member_id');
	    foreach ($ids as $value) {
	        $ids = array_merge($ids, self::getChildsId($value));
	    }
	    return $ids;
	}
	/**
	 * 获取所有前辈
	 * @param int $id 用户id
	 * @author PaulXun <paulxun@qq.com>
	 * @return array
	 */
	public function getFathers($id = 0, $group = 0)
	{
	    $father = self::where('member_id', $id)->find();
		if ($father) {
			if ($father['group'] > $group) $this->user[] = $father->toArray();
			if ($father['pid'] > 0) self::getFathers($father['pid'], $father['group']);
		}
	    return $this->user;
	}
	/**
	 * 获取团队
	 * @param int $pid 父级id
	 * @author PaulXun <paulxun@qq.com>
	 * @return array
	 */
	public static function getGroupList($pid = 0)
	{
	    $ids = self::where('pid', $pid)->column('member_id');
	    foreach ($ids as $value) {
	        $ids = array_merge($ids, self::getChildsId($value));
	    }
	    return $ids;
	}
	/**
	 * 获取会员数据
	 * @param int $id 会员id
	 * @return mixed|static
	 */
	public static function info($id = 0)
	{
	    $result = cache('vip_'.$id);
	    if (!$result) {
	        $result = self::where('member_id', $id)->find();
	        // 非开发模式，缓存数据
	        if (config('app.develop_mode') == 0) {
	            cache('vip_'.$id, $result);
	        }
	    }
	    return $result;
	}
	
	/**
	 * 获取错误信息
	 * @access public
	 * @return mixed
	 */
	public function getError()
	{
	    return $this->error;
	}
}
